package com.sduept.nwld.dataserver.interceptors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.nwld.fault.outer.manager.FaultToOMSServerManager;

@Aspect
@Service
public class FaultRecordToOMSInterceptor {
	@Autowired
	private FaultToOMSServerManager faultToOMSServerManager;

	private static  final ExecutorService executor=Executors.newFixedThreadPool(3);

	@Pointcut("execution(* com.sduept.bigdata.fault.manager.FaultRecordPersistManager.deleteFaultRecord(..))")
	public void declareJoinPointExpression(){

	}

	@Around(value= "declareJoinPointExpression()")
	public Object methodInterceptor(final ProceedingJoinPoint ctx) throws Exception {
		Object returnObject = null;
		try {
			returnObject = ctx.proceed();
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		Object finalReturnObject = returnObject;
		executor.execute(new Runnable() {
			@Override
			public void run() {
				String methodName = ctx.getSignature().getName();//方法名
				if("deleteFaultRecord".equals(methodName)){
					if(finalReturnObject instanceof FaultRecord){
						FaultRecord fr = (FaultRecord) finalReturnObject;

						faultToOMSServerManager.sendFaultToOmsServer(fr,1);
					}
				}else{
					if(finalReturnObject instanceof FaultRecord){
						FaultRecord fr = (FaultRecord) finalReturnObject;
						faultToOMSServerManager.sendFaultToOmsServer(fr,0);
					}
				}
			}
		});
		return returnObject;
	}
}
